/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-06-30 17:25:41
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-07-04 09:19:39
 * @FilePath: \A06_From_A05\Device\epwm.c
 * @Description: EPWM配置
 */
#include "dev.h"


static void epwm_gpio_config(void){
    GPIO_InitTypeStruct gpioEPWM;
    gpioEPWM.GPIO_Pin = L_BAT_PWM_PIN;
    gpioEPWM.GPIO_Mode = GPIO_MODE_ALT_FUNC;
    gpioEPWM.GPIO_OType = GPIO_OTYPE_PP;
    gpioEPWM.GPIO_Speed = GPIO_SPEED_HIGH;
    gpioEPWM.GPIO_PuPd = GPIO_PUPD_NOPULL;

    GPIO_Init(L_BAT_PWM_PORT , &gpioEPWM);

    gpioEPWM.GPIO_Pin = R_BAT_PWM_PIN;
    GPIO_Init(R_BAT_PWM_PORT , &gpioEPWM);

    GPIO_AltFuncConfig(L_BAT_PWM_PORT , L_BAT_PWM_PIN , L_BAT_EPWM_AF);
    GPIO_AltFuncConfig(R_BAT_PWM_PORT , R_BAT_PWM_PIN , R_BAT_EPWM_AF);
    GPIO_ResetBit(L_BAT_PWM_PORT , L_BAT_PWM_PIN);
    GPIO_ResetBit(R_BAT_PWM_PORT , R_BAT_PWM_PIN);
}

/**
 * @description: epwm初始化
 * @return {*}
 */
void epwm_config(void){
    epwm_gpio_config();

    /* Config EPWM_TIM */
    EPWM_TimeBaseInitTypeStruct EPWM_TimeBaseInitStruct;
    EPWM_TimeBaseStructInit(&EPWM_TimeBaseInitStruct);
    EPWM_TimeBaseInitStruct.EPWM_ClockDiv = EPWM_CLK_DIV1;                  /*不分频*/
    EPWM_TimeBaseInitStruct.EPWM_CounterCycle = EPWM_COUNTER_CYCLE_PERIOD;  /*周期计数*/
    EPWM_TimeBaseInitStruct.EPWM_CounterMode = EPWM_COUNTER_MODE_INCREASE;  /*递增*/
    EPWM_TimeBaseInitStruct.EPWM_ReloadValue = EPWM_PERIOD;
    EPWM_TimeBaseInit(&EPWM_TimeBaseInitStruct);
    /* Enable EPWM */
    EPWM_Cmd(ENABLE);
    /* Config EPWM_CH */
    EPWM_CHInitTypeStruct EPWM_CHInitStruct;
    EPWM_CHStructInit(&EPWM_CHInitStruct);
    EPWM_CHInitStruct.EPWM_Compare1Value = 0;
    EPWM_CHInitStruct.EPWM_Compare2Value = 0;
    EPWM_CHInitStruct.EPWM_CHInitStatus = EPWM_CH_INIT_STATUS_1;
    EPWM_CHInitStruct.EPWM_Compare1Action = EPWM_CMP_ACT_OUTPUT_LOW;   /*计数值等于比较值时输出翻转为低*/
    EPWM_CHInitStruct.EPWM_Compare2Action = EPWM_CMP_ACT_OUTPUT_UNCHANGE;
    EPWM_CHInitStruct.EPWM_NPolarity = EPWM_CH_OUT_POLARITY_NORMAL;
    EPWM_CHInitStruct.EPWM_PPolarity = EPWM_CH_OUT_POLARITY_NORMAL;
    EPWM_CHInitStruct.EPWM_Compare1Dir = EPWM_CMP_DIR_INCREASE;
    EPWM_CHInitStruct.EPWM_Compare2Dir = EPWM_CMP_DIR_DECREASE;
    EPWM_CHInit(L_BAT_EPWM_CHANNEL, &EPWM_CHInitStruct);

    EPWM_CHInitStruct.EPWM_NPolarity = EPWM_CH_OUT_POLARITY_INVERT;
    EPWM_CHInitStruct.EPWM_PPolarity = EPWM_CH_OUT_POLARITY_NORMAL;
    EPWM_CHInit(R_BAT_EPWM_CHANNEL, &EPWM_CHInitStruct);

    EPWM_CHCmd(L_BAT_EPWM_CHANNEL , DISABLE);
    EPWM_CHCmd(R_BAT_EPWM_CHANNEL , DISABLE);
}
